package com.qf.oa_system.controller;

import com.qf.oa_system.entity.User;
import com.qf.oa_system.entity.UserRoles;
import com.qf.oa_system.service.UserRolesService;
import com.qf.oa_system.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2020-06-03 20:26:32
 */
@Controller
@RequestMapping("/user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    @Resource
    private UserRolesService userRolesService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public User selectOne(Integer id) {
        return this.userService.queryById(id);
    }

    @RequestMapping("/loginpage")
    public String loginUI(){
        return "login";
    }

    @RequestMapping("/registerpage")
    public String registerUI(){
        return "register";
    }

    @RequestMapping("/register")
    public String register(User user){
        //shiro框架生成随机数
        String salt = new SecureRandomNumberGenerator().nextBytes().toString();//生成随机数
        int times = 2;
        String algorithmName = "md5";
        String encodedPassword = new SimpleHash(algorithmName,user.getUserPassword(),salt,times).toString();

        User u = new User();
        u.setUserName(user.getUserName());
        u.setUserPassword(encodedPassword);
        u.setSalt(salt);
        u.setImage("/img/avatar-1.jpg");

        User us = userService.insert(u);

        UserRoles ur = new UserRoles();
        ur.setUserId(us.getUserId());
        ur.setRoleId(5);

        userRolesService.insert(ur);

        return "login";
    }

    @RequestMapping("/login")
    public String login(User user, Model model){
        //shiro框架获取用户对象
        Subject subject = SecurityUtils.getSubject();
        //存储前端传过来的用户名及密码
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(),user.getUserPassword());
        try {
            subject.login(token);
            System.out.println("登录成功");
            Session session=subject.getSession();
            User user1 = userService.queryByName(user.getUserName());
            session.setAttribute("subject", subject);
            if (subject.hasRole("admin")) {
                session.setAttribute("role","admin");
            }else if (subject.hasRole("principal")){
                session.setAttribute("role","principal");
            }else if (subject.hasRole("headmaster")){
                session.setAttribute("role","headmaster");
            }else if (subject.hasRole("teacher")){
                session.setAttribute("role","teacher");
            }else if (subject.hasRole("student")){
                session.setAttribute("role","student");
            }
            session.setAttribute("user",user1);
            session.setAttribute("filename",user1.getImage());
        } catch (AuthenticationException e) {
            model.addAttribute("error", "验证失败！");
            return "/login";
        }
        return "index";
    }

    @RequestMapping("/indexUI")
    public String indexUI(){
        return "index";
    }

}